package day28;

class Solution {
    public static void main(String[] args) {
        int[]arr=new int[]{2,3,1,2,4,3};
        minSubArrayLen(7, arr);
    }
    public static int minSubArrayLen(int target, int[] nums) {
        //解题思路：使用滑动窗口
        int left=0;
        int right=0;
        int preLeft=0;//遍历结束的条件
        int preRight=0;
        int length=Integer.MAX_VALUE;
        int sum=0;//用来记录滑动窗口的总和
        while(preLeft!=left||preRight!=right){
            preLeft=left;//记录当前left和right的位置
            preRight=right;
            while(sum<target&&right<nums.length){
                sum+=nums[right];
                right++;
            }
            while(sum>=target&&left<=right){
                if(sum==target){
                    int len=right-left;
                    if(len<length){
                        length=len;
                    }
                }
                sum-=nums[left];
                left++;
                
            }
        }
        return length;
    }
}